package com.yan.test;

import org.apache.commons.codec.binary.Hex;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Base64;

/**
 * Created by yanl_ on 2018/2/28.
 */
public class DataEncryptUtil {

    public static String getBase64Encoder(String param, String encoding) {
        String content = null;
        try {
            content = Base64.getEncoder().encodeToString(param.getBytes(encoding));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return content;
    }

    public static String getBase64Decoder(String param, String encoding) {

        String content = null;
        byte[] bytes = Base64.getDecoder().decode(param);

        try {
            content = new String(bytes, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return content;
    }


    public static String getSHAStr(String str, String encodeType) {
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance(encodeType);
            byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
            encdeStr = Hex.encodeHexString(hash);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encdeStr;
    }

    public static void main(String[] args) {


        String content = "94960ece-9513-4265-9cf2-67a4da330213.";
        if (content.lastIndexOf(".") > 0) {
            String charparma = content.substring(content.length() - 1, content.length());
            content = content.replace(charparma, ":");
            System.out.println(content);
        }
        System.out.println(getBase64Encoder("94960ece-9513-4265-9cf2-67a4da330213:", "utf-8"));

        System.out.println(getBase64Decoder("OTQ5NjBlY2UtOTUxMy00MjY1LTljZjItNjdhNGRhMzMwMjEzOg==", "utf-8"));


        System.out.println(getBase64Encoder("SB-Mid-server-6ueQbOHFD9a7K92nEEeDkgez:", "utf-8"));
        System.out.println(getBase64Encoder("SB-Mid-server-6ueQbOHFD9a7K92nEEeDkgez", "utf-8"));

        System.out.println(getSHAStr("yan", "SHA-512"));
    }
}
